package com;

import org.geotools.api.data.FileDataStore;
import org.geotools.api.data.FileDataStoreFinder;
import org.geotools.api.feature.simple.SimpleFeature;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;

import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 解析shp文件
 */
public class ParseShp {

    public static void main(String[] args) {
        String shpPath = "C:\\Users\\hasee\\Desktop\\2021年矢量\\2021年矢量\\2021年矢量数据.shp";
        try {
            parseShpToWkt(shpPath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public static void parseShpToWkt(String shpPath) throws Exception {
        File file = new File(shpPath);
        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureCollection collection = store.getFeatureSource().getFeatures();

        try (SimpleFeatureIterator iterator = collection.features()) {
            AtomicInteger count = new AtomicInteger(0);
            while (iterator.hasNext()) {
                SimpleFeature feature = iterator.next();
                String wkt = feature.getDefaultGeometry().toString();
//                System.out.println("Feature ID: " + feature.getID());
//                System.out.println("WKT Geometry: " + wkt);
//                System.out.println("Attributes: " + feature.getAttributes());
                int i = count.incrementAndGet();
                System.out.println(i);
            }
            System.out.println(String.format("总数为：d%", count));
        }
        store.dispose();
    }
}
